package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

//购物车管理
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;
    //  加入购物车
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
        //返回值  库存对象
        CartInfo cartInfo = cartFeignClient.toCart(skuId);
       // cartInfo.setSkuNum(skuNum);
      //  model.addAttribute("cartInfo",cartInfo);
        redirectAttributes.addAttribute("pid",skuId);
        redirectAttributes.addAttribute("sn",skuNum);
        return "redirect:http://cart.gmall.com/toCart.html";
    }

    //重定向到     购物车添加成功页面
    @GetMapping("/toCart.html")
    public String toCart(Long pid, Integer sn, Model model){
        //返回值  库存对象
        CartInfo cartInfo = cartFeignClient.toCart(pid);
        cartInfo.setSkuNum(sn);
        model.addAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }

}
